package com.gexiaobao.pigeon.ui.fragment.mine.pigeon.activity

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.ListPopupWindow
import androidx.core.view.isVisible
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.model.bean.PigeonRaceCharResponse
import com.gexiaobao.pigeon.app.util.LineChartManager
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.ActivityForceAnalysisBinding
import com.gexiaobao.pigeon.ui.adapter.AddBloodPigeonPopAdapter
import com.gexiaobao.pigeon.viewmodel.MyPigeonViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.jessyan.autosize.internal.CustomAdapt
import java.util.Collections


/**
 * @Author      : hxw
 * @Date        : 2024/3/18 11:14
 * @Describe    : 赛鸽战绩统计
 */
class ActivityForceAnalysis : BaseActivity<MyPigeonViewModel, ActivityForceAnalysisBinding>(), CustomAdapt {

    //页面适配
    private var landScreenBoolean = false
    private var landScreenFloat: Float = 420.0F
    private var landScreenFloatHW: Float = 420.0F
    private var landScreenFloatNok: Float = 410.0F
    private var landScreenFloatS: Float = 760.0F

    //请求数据相关
    private var mPigeonId = 0
    private var mRaceId = 0
    private var mRaceType = 0

    //折线图相关
    private val xAxisData2: MutableList<String> = arrayListOf()//存储X轴数据
    private var maxValue: Float = 0.00f
    private lateinit var mLineChartManage: LineChartManager

    private var adapterRace: AddBloodPigeonPopAdapter? = null
    private var mListPopForce: ArrayList<String> = arrayListOf()
    private var mPigeonRacesBeanList: ArrayList<PigeonRaceCharResponse.PigeonRacesBean> = arrayListOf()
    private val popWindowRace: ListPopupWindow by lazy { ListPopupWindow(this) }
    private var mRacePosition = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        StatusBarUtil.setLightMode(this)
    }

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        val ringId = intent.getStringExtra("ringId")
        mRaceId = intent.getIntExtra("raceId", 0)
        mRaceType = intent.getIntExtra("raceType", 0)
        mDatabind.tvWhiteTitle.text = ringId + "分速折线图"
        mPigeonId = intent.getStringExtra("pigeonId")!!.toInt()

        getForceAnalysisData(mPigeonId)

        popWindowRace.setOnItemClickListener { _, _, position, _ ->
            mRacePosition = position
            adapterRace!!.setCheckItem(position)
            mDatabind.tvSpinner.text = mPigeonRacesBeanList[position].raceName
            mRaceId = mPigeonRacesBeanList[position].raceId
            mRaceType = mPigeonRacesBeanList[position].raceType
            removeLineChart()
            getForceAnalysisData(mPigeonId)
            popWindowRace.dismiss()
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.pigeonForceResult.observe(this) { it ->
            if (it != null) {
                mDatabind.data = it
                if (it.me?.chartGrades?.isEmpty() == true && it.other?.chartGrades?.isEmpty() == true) {
                    mDatabind.tvNoData.isVisible = true
                    mDatabind.lineChart.isVisible = false
                } else {
                    mDatabind.tvNoData.isVisible = false
                    mDatabind.lineChart.isVisible = true
                    mLineChartManage = LineChartManager(mDatabind.lineChart)
                    mLineChartManage.initLineChart()
                    val listMine: MutableList<Float> = arrayListOf()
                    val listChampion: MutableList<Float> = arrayListOf()
                    if (it.me != null && it.me!!.chartGrades.isNotEmpty()) {
                        it.me!!.chartGrades.forEachIndexed { _, chartGradesBean ->
                            listMine.add(chartGradesBean.speed)
                        }
                    }
                    if (it.other != null && it.other!!.chartGrades.isNotEmpty()) {
                        it.other!!.chartGrades.forEach {
                            listChampion.add(it.speed)
                        }
                    }
                    //取最大值
                    var meMax = 0.00f
                    var otherMax = 0.00f
                    if (listMine.isNotEmpty()) {
                        meMax = Collections.max(listMine)
                    }
                    if (listChampion.isNotEmpty()) {
                        otherMax = Collections.max(listChampion)
                    }
                    val result = if (meMax > otherMax) {
                        meMax
                    } else {
                        otherMax
                    }
                    maxValue = Util.add(result.toDouble(), 500.00).toFloat()
                    mLineChartManage.setUpLineChart(xAxisData2, it, maxValue)
                }
            }
            setRaceList(it)
        }
    }

    private fun setRaceList(it: PigeonRaceCharResponse) {
        mListPopForce.clear()
        if (it.pigeonRaces.isNotEmpty()) {
            mPigeonRacesBeanList.clear()
            mPigeonRacesBeanList.addAll(it.pigeonRaces)
            it.pigeonRaces.forEach {
                mListPopForce.add(it.raceName)
                if (it.raceId == mRaceId) {
                    mDatabind.tvSpinner.text = it.raceName
                }
            }
        }
        adapterRace = AddBloodPigeonPopAdapter(this, mListPopForce)
        popWindowRace.setAdapter(adapterRace)
        popWindowRace.anchorView = mDatabind.tvSpinner//设置ListPopupWindow的锚点，关联mButton位置
        popWindowRace.width = LinearLayout.LayoutParams.WRAP_CONTENT
        popWindowRace.height = LinearLayout.LayoutParams.WRAP_CONTENT
        popWindowRace.isModal = true
        adapterRace!!.setCheckItem(mRacePosition)
    }

    private fun getForceAnalysisData(pigeonId: Int) {
        showLoading(getString(R.string.loading))
        mViewModel.getPigeonRaceChart(pigeonId, mRaceId, mRaceType)
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.ivBack, mDatabind.tvSpinner) {
            when (it) {
                mDatabind.tvSpinner -> popWindowRace.show()
                mDatabind.ivBack -> {
                    finish()
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        removeLineChart()
    }

    private fun removeLineChart() {
        mDatabind.lineChart.removeAllViews()
        mDatabind.lineChart.clearAllViewportJobs()
        mDatabind.lineChart.removeAllViewsInLayout()
    }

    override fun isBaseOnWidth(): Boolean {
        return if (Build.MANUFACTURER == "realme" || Build.MANUFACTURER == "vivo" || Build.MANUFACTURER == "OPPO"
            || Build.MANUFACTURER == "HUAWEI" || Build.MANUFACTURER == "HMD Global"
        ) {
            landScreenBoolean
        } else {
            !landScreenBoolean
        }
    }

    override fun getSizeInDp(): Float {
        return when (Build.MANUFACTURER) {
            "realme", "vivo", "OPPO" -> {
                landScreenFloat
            }

            "HUAWEI" -> {
                landScreenFloatHW
            }

            "HMD Global" -> {
                landScreenFloatNok
            }

            else -> {
                landScreenFloatS
            }
        }
    }
}